package com.ego.item.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ego.exception.enums.ExceptionEnum;
import com.ego.exception.exception.EgoException;
import com.ego.item.entity.Category;
import com.ego.item.mapper.CategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 根据parentId查询子类目
     *
     * @param pid
     * @return
     */
    @Transactional
    public List<Category> queryCategoryListByParentId(Long pid) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", pid);
        return this.categoryMapper.selectList(queryWrapper);
    }

    /**
     * 新增节点
     * @param category
     */
    @Transactional
    public void save(Category category) {
        int i = categoryMapper.insert(category);
        if (i == 0){
            throw new EgoException(ExceptionEnum.CATEGORY_SAVE_ERROR);
        }
    }

    /**
     * 根据id更新节点
     * @param id
     * @param name
     */
    public void update(Long id, String name) {
        Category category = categoryMapper.selectById(id);
        category.setName(name);
        int i = categoryMapper.updateById(category);
        if (i == 0){
            throw new EgoException(ExceptionEnum.CATEGORY_UPDATE_ERROR);
        }
    }

    /**
     * 根据id删除节点
     * @param id
     */
    public void delete(Long id) {
        int i = categoryMapper.deleteById(id);
        if (i == 0){
            throw new EgoException(ExceptionEnum.CATEGORY_DELETE_ERROR);
        }
    }
    @Transactional(readOnly = true)
    public List<Category> queryByBrandId(Long bid) {
        return categoryMapper.queryByBrandId(bid);
    }


    @Transactional(readOnly = true)
    public List<Category> findListByIds(List<Long> ids) {
        return categoryMapper.selectBatchIds(ids);
    }

    public List<Category> findListByIdList(List<Long> categoryIdList) {
        return categoryMapper.selectBatchIds(categoryIdList);

    }
}
